package com.zbkj.crmeb.im.utils;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.log.StaticLog;
import com.alibaba.fastjson.JSONObject;
import com.common.PageParamRequest;
import com.crmeb.core.utils.RedisUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
public class ImAddressUtil {

    @Resource
    private ImAddressConfig config;
    @Resource
    protected RedisUtil redisUtil;

    public static final String TOKEN = "im_address_token";

    public String getToken() throws Exception {
        Assert.notEmpty(config.getApiPath(), "请在阿波罗配置 im.appKey");
        Assert.notEmpty(config.getAppKey(), "请在阿波罗配置 im.appSecret");
        Assert.notEmpty(config.getAppSecret(), "请在阿波罗配置 im.url.domain");
      /*  String redisToken = (String) redisUtil.get(TOKEN);
        if (StrUtil.isNotEmpty(redisToken)) return redisToken;*/
        String path = config.getApiPath() + "/apis/v1/token";
        Map<String, Object> params = new HashMap<>();
        params.put("appKey", config.getAppKey());
        params.put("appSecret", config.getAppSecret());
        String respStr = HttpUtil.post(path, JSONObject.toJSONString(params));
        StaticLog.info("im获取token:{}", respStr);
        JSONObject resp = JSONObject.parseObject(respStr);
        String code = resp.getString("code");
        if ("1".equals(code)) {
            JSONObject data = resp.getJSONObject("data");
            String token = data.getString("access_token");
            //Long expiresin = data.getLong("expiresin");
           // redisUtil.set(TOKEN, token, 60l, TimeUnit.SECONDS);
            return token;
        }
        String error = resp.getString("error");
        throw new RuntimeException(error);
    }

    public JSONObject getAddressList(PageParamRequest request) throws Exception {
        String token = getToken();
        String path = config.getApiPath() + "/apis/v1/channel/address/list";
        Integer pageNo = request.getPage();
        if (null == pageNo) pageNo = 1;
        Integer pageSize = request.getLimit();
        if (null == pageSize) pageSize = 50;
        if (pageSize > 50) throw new RuntimeException("分页大小限制均为50以内");
        cn.hutool.json.JSONObject params = new cn.hutool.json.JSONObject();
        params.put("pageNo", pageNo);
        params.put("pageSize", pageSize);
        //access-token
        String respStr = HttpRequest.post(path).header("access-token", token).body(params).timeout(HttpRequest.TIMEOUT_DEFAULT).execute().body();
        StaticLog.info("im获取地址:{}", respStr);
        JSONObject resp = JSONObject.parseObject(respStr);
        String code = resp.getString("code");
        if ("1".equals(code)) {
            JSONObject data = resp.getJSONObject("data");
            return data;
        }
        String error = resp.getString("error");
        throw new RuntimeException(error);
    }


}
